//
//  KTNewFetureCVC.m
//  KTLotteryTicket
//
//  Created by CoolKi on 16/3/7.
//  Copyright © 2016年 CoolKi. All rights reserved.
//

#import "KTNewFetureCVC.h"
#import "KTNewCollectionCell.h"

@interface KTNewFetureCVC ()
//上一次的offset.x
@property (nonatomic ,assign) CGFloat preOffsetX;

//guide1ImageV
@property (nonatomic ,weak) UIImageView * guide1ImageV;

@property (nonatomic ,weak) UIImageView *largeTextImageV;

@property (nonatomic ,weak) UIImageView *smallTextImageV;

@end

@implementation KTNewFetureCVC


static NSString * const reuseIdentifier = @"KTCollectionCell";//系统自动写好的

-(instancetype)init{
    //设置流水布局
    UICollectionViewFlowLayout * flowLyout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置每一个格子的大小
    flowLyout.itemSize = [UIScreen mainScreen].bounds.size;
    //设置最小行间距
    flowLyout.minimumLineSpacing = 0;
    //设置每个格子之间的间距
    flowLyout.minimumInteritemSpacing = 0;
    //设置滚动的方向/水平方向
    flowLyout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //初始化并带有 流水布局
    return [super initWithCollectionViewLayout:flowLyout];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes//注册自定义 KTCollectionCell
    [self.collectionView registerClass:[KTNewCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];//系统自动写好的
    
    // Do any additional setup after loading the view.
    
    //是否 整页滑动
    self.collectionView.pagingEnabled = YES;
    
    //是否 弹簧效果
    self.collectionView.bounces = NO;
    
    
    [self setUp];
}

- (void)setUp {
    //guideLine
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    imageV.x -= 150;
    [self.collectionView addSubview:imageV];
    
    //guide1
    UIImageView *guide1ImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    guide1ImageV.x += 50;
    self.guide1ImageV = guide1ImageV;
    [self.collectionView addSubview:guide1ImageV];
    
    //guideLargeText1
    //guide1
    UIImageView *largeTextImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largeTextImageV.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.6);
    self.largeTextImageV = largeTextImageV;
    [self.collectionView addSubview:largeTextImageV];
    
    //guideSmallText1
    UIImageView *smallTextImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    smallTextImageV.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.7);
    self.smallTextImageV = smallTextImageV;
    [self.collectionView addSubview:smallTextImageV];
    
}

//当scorllVeiw减速时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //~~~~~~~~
    CGFloat offset = scrollView.contentOffset.x - self.preOffsetX;
    
    self.preOffsetX = scrollView.contentOffset.x;
    self.guide1ImageV.x += 2 * offset;
    
    self.largeTextImageV.x += 2 * offset;
    
    self.smallTextImageV.x += 2 * offset;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.guide1ImageV.x -= offset;
        self.largeTextImageV.x -= offset;
        self.smallTextImageV.x -= offset;
    }];
    
    // NSLog(@"--offset--%f",offset);
    
    //计算当前是多少页
    int page = scrollView.contentOffset.x / self.collectionView.width;
    
    //给guide1ImageV重新设置图片
    
    self.guide1ImageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d",page + 1]];
    self.largeTextImageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideLargeText%d",page + 1]];
    self.smallTextImageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideSmallText%d",page + 1]];
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // Configure the cell
    KTNewCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    NSString * imgName = [NSString stringWithFormat:@"guide%ldBackground568h",indexPath.item+1];
    [cell setJoinBtnWithCurrentIndexPath:indexPath countItems:4];
    cell.img = [UIImage imageNamed:imgName];
    [cell.contentView bringSubviewToFront:cell.startBtn];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
